package com.dunqi.gpm.cangxi.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseFragment;
import com.dunqi.gpm.cangxi.common.base.ComObserver;
import com.dunqi.gpm.cangxi.common.base.Data;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.home.activity.DayReportActivity;
import com.dunqi.gpm.cangxi.home.adapter.HomeProjectAdapter;
import com.dunqi.gpm.cangxi.home.bean.Page;
import com.dunqi.gpm.cangxi.home.bean.Project;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.widget.EmptyView;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Created by 空 on 2017/11/17 0017.
 */

public class ProjectListDayReportFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private static final String DATA = "types";
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    ArrayList<Project> data = new ArrayList<>();
    private HomeProjectAdapter adapter;
    private HashMap<String, String> params;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        params.put(Constance.PAGE, String.valueOf(page));
        ApiFactory.getInstance()
                .selectProjects(params)
                .compose(RxSchedulers.<Data<Page<Project>>>io_main())
                .subscribe(new ComObserver<Page<Project>>() {
                    @Override
                    public void onSuccess(Page<Project> projectPage) {
                        ArrayList<Project> list = projectPage.getList();

                        if (page == 1) {
                            data.clear();
                        }

                        data.addAll(list);
                        page++;
                        adapter.notifyDataSetChanged();

                        if (projectPage.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
//                            return;
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        adapter.loadMoreFail();
                    }

                    @Override
                    public void onFinal() {
                        swipeRefresh.setRefreshing(false);
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposables(d);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {
        SwipeUtil.init(swipeRefresh);

        recycler.setLayoutManager(new LinearLayoutManager(context));

        adapter = new HomeProjectAdapter(data);
        recycler.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        adapter.setEmptyView(new EmptyView(context));

        adapter.setOnLoadMoreListener(this, recycler);

        swipeRefresh.setOnRefreshListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_all_project;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Project project = data.get(position);
        DayReportActivity.start(getContext(), project.getProjectId());
    }

    public static ProjectListDayReportFragment newInstance(HashMap<String, String> params) {
        ProjectListDayReportFragment projectListFragment = new ProjectListDayReportFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(DATA, params);
        projectListFragment.setArguments(bundle);
        return projectListFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            params = (HashMap<String, String>) arguments.getSerializable(DATA);
            params.put(Constance.PAGE, String.valueOf(page));
//            params.put("enabled", String.valueOf(1));
        }
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

}
